Skip to content

fix(binder): include field name in bind conversion errors (#2629)#3005

Merged
vishr merged 1 commit into
masterfrom
fix-2629-bind-field-name
Jun 13, 2026
Merged

fix(binder): include field name in bind conversion errors (#2629)#3005
vishr merged 1 commit into
masterfrom
fix-2629-bind-field-name

Conversation

@vishr

@vishr vishr commented Jun 13, 2026

Copy link
Copy Markdown
Member

Problem (#2629)

When c.Bind() fails a type conversion on form/struct data, the error gives no indication of which field failed:

POST /submit  number=10a
→ code=400, message=Bad Request, err=strconv.ParseInt: parsing "10a": invalid syntax

This makes it hard to render a useful validation message ("the number field must be an integer").

Fix

bindData already has the field name (inputFieldName) in scope at each conversion site but returned the bare error. Wrap those returns with the field name using %w (so errors.Is/errors.As still work):

→ code=400, message=Bad Request, err=number: strconv.ParseInt: parsing "10a": invalid syntax

This is the wrap the reporter proposed in the thread (fmt.Errorf("%s: %w", inputFieldName, err)), applied to all four conversion-error sites (unmarshaler, slice element, scalar).

Test

TestBind_formConversionErrorIncludesFieldName (written first; fails on master — error contains no field name). Four existing tests that asserted the bare message are updated to the field-prefixed form. gofmt/vet clean; full root-package suite passes.

Fixes #2629.

🤖 Generated with Claude Code

…#2629)

bindData had the field name (inputFieldName) in scope but returned bare
conversion errors, so c.Bind() failures gave no indication of which field
failed (e.g. "strconv.ParseInt: parsing \"10a\"..."). Wrap the conversion
errors with the field name using %w so errors.Is/As still work.

Matches the wrap proposed by the reporter in the issue thread. Existing
tests that asserted the bare message are updated to the field-prefixed form.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vishr vishr merged commit ec9515a into master Jun 13, 2026
8 checks passed
@vishr vishr deleted the fix-2629-bind-field-name branch June 13, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When c.bind() fails with a type error on POST'd form data the inputfieldname is not available

1 participant